package com.ytxg.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ytxg.practice.dto.ParkDto;
import com.ytxg.practice.mapper.AdminMapper;
import com.ytxg.practice.mapper.DiquMapper;
import com.ytxg.practice.mapper.ParkMapper;
import com.ytxg.practice.po.Admin;
import com.ytxg.practice.po.Diqu;
import com.ytxg.practice.po.Park;
import com.ytxg.practice.service.AdminService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private ParkMapper parkMapper;
    @Resource
    private DiquMapper diquMapper;

    @Override
    public Admin login(String name, String password) {
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Admin::getName, name)
                .eq(Admin::getPassword, password);
        Admin admin = adminMapper.selectOne(lambdaQueryWrapper);
        if (admin == null) {
            return null;
        }
        return admin;
    }

    @Override
    public List<ParkDto> getCarList() {
        List<Park> carList = parkMapper.getCarList();
        List<ParkDto> parkDtos = toParkDtos(carList);
        return parkDtos;
    }

    @Override
    public List<Diqu> getDiQuList() {
        List<Diqu> diquList = diquMapper.getDiquList();
        return diquList;
    }

    @Override
    public Map<String, String> addCar(Park car) {
        Map<String, String> map = new HashMap<>();
        try {
            parkMapper.addCar(car);
            map.put("res", "0");
        } catch (Exception e) {
            map.put("res", "1");
        }

        return map;
    }

    @Override
    public Map<String, String> delCar(Integer carId) {
        HashMap<String, String> map = new HashMap<>();
        try {
            parkMapper.delCar(carId);
            map.put("res", "0");
        } catch (Exception e) {
            map.put("res", "1");
        }

        return map;
    }

    @Override
    public Park editCar(Integer carId) {
        Park oneCar = parkMapper.getOneCar(carId);
        return oneCar;
    }

    @Override
    public Map<String, String> carUpdate(Park car) {
        HashMap<String, String> map = new HashMap<>();
        try {
            parkMapper.carUpdate(car);
            map.put("res", "0");
        } catch (Exception e) {
            map.put("res", "1");
        }
        return map;

    }

    @Override
    public List<ParkDto> findCarByKey(String key) {
        List<Park> carByKey = parkMapper.findCarByKey("%" +key+ "%");
        List<ParkDto> parkDtos = toParkDtos(carByKey);
        return parkDtos;

    }


    private List<ParkDto> toParkDtos(List<Park> parks){
        List<ParkDto> parkDtos = new ArrayList<>();
        for (Park park : parks) {
            ParkDto build = ParkDto.builder()
                    .diquId(park.getId())
                    .jingduX(park.getJingduX())
                    .jingduY(park.getJingduY())
                    .address(park.getAddress())
                    .diquName(diquMapper.getDiqu(park.getDiquId()))
                    .price(park.getPrice())
                    .status(park.getStatus())
                    .id(park.getId())
                    .name(park.getName())
                    .build();
            parkDtos.add(build);
        }
        return parkDtos;
    }
}
